using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class CatMachineItem : BaseItem
	{
		public enum State
		{
			Close = 0,
			Open = 1
		}

		public enum Anim
		{
			Static = 0,
			Open = 1,
			Close = 2
		}

		private const float OPEN_POSITION = 1.42f;

		public State state;

		public Anim anim;

		private float animTimer;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			state = (State)data.state;
			if (state == State.Open)
			{
				model.SetLocalX(1.42f);
			}
		}

		protected override void LateUpdate()
		{
			if (anim != 0)
			{
				animTimer += Time.deltaTime;
				if (anim == Anim.Open)
				{
					model.SetLocalX(Mathf.Lerp(0f, 1.42f, animTimer));
				}
				else
				{
					model.SetLocalX(Mathf.Lerp(1.42f, 0f, animTimer));
				}
				if (animTimer >= 1f)
				{
					anim = Anim.Static;
				}
			}
		}

		public override void QuicklyTouch(TouchEventData touchEventData)
		{
			base.QuicklyTouch(touchEventData);
			if (anim != 0)
			{
				return;
			}
			if (state == State.Close)
			{
				anim = Anim.Open;
				state = State.Open;
				Singleton<AudioManager>.Instance.PlaySound("sound_cat_door_move_open");
				CatMachineBoxItem catMachineBoxItem = (CatMachineBoxItem)Singleton<Scene>.Instance.GetItemByScriptType(81);
				if (catMachineBoxItem != null)
				{
					catMachineBoxItem.SetState(CatMachineBoxItem.State.Open);
				}
			}
			else
			{
				anim = Anim.Close;
				state = State.Close;
				Singleton<AudioManager>.Instance.PlaySound("sound_cat_door_move_close");
				CatMachineBoxItem catMachineBoxItem2 = (CatMachineBoxItem)Singleton<Scene>.Instance.GetItemByScriptType(81);
				if (catMachineBoxItem2 != null)
				{
					catMachineBoxItem2.SetState(CatMachineBoxItem.State.Close);
				}
			}
			animTimer = 0f;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["33"] = (int)state;
		}
	}
}
